package com.example.xiaomi.Model.Service;

import com.example.xiaomi.Model.Controller.AddressList;
import com.example.xiaomi.Model.Controller.GodsList;
import com.example.xiaomi.Model.entity.GodsEntity;
import com.example.xiaomi.Model.entity.UeserEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/product/details")
public class Product_details extends HttpServlet {
    GodsList godsList = new GodsList();
    AddressList AddressList = new AddressList();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        request.setAttribute("commodityid",id);
        String name = request.getParameter("name");
        List<GodsEntity> specify =  godsList.specify(Integer.parseInt(id));
        for (GodsEntity list: specify) {
            request.setAttribute("commodity_Name",list.getGname());
            request.setAttribute("commodity_Price",list.getGprice());
            String[] arr = list.getPing().split(",");
            request.setAttribute("size_Img",arr.length);
            request.setAttribute("commodity_Ping",list.getPing());
            System.out.println(list.getGdetails());
            request.setAttribute("commodity_Details",list.getGdetails().replace("\"",""));
            break;
        }
        request.setAttribute("name",name);
        String user_Count = request.getParameter("userid");
        if (user_Count != null){
            request.setAttribute("address",AddressList.selectall(Integer.parseInt(user_Count)));
        }

        List<GodsEntity> list = godsList.selectall();
        String[][] arr = new String[list.size()][6];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {

                if (j == 0){
                    arr[i][j] = list.get(i).getGname();
                }else if (j == 1){
                    arr[i][j] = String.valueOf(list.get(i).getGprice());
                }else if (j == 2){
                    arr[i][j] = list.get(i).getPing();
                }else if (j == 3){
                    arr[i][j] = list.get(i).getIs_sort();
                }else if (j == 4){
                    arr[i][j] = list.get(i).getSname();
                }else if (j == 5){
                    arr[i][j] = String.valueOf(list.get(i).getSid());
                }
            }
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < arr.length; i++ ){
            for(int j = 0; j < arr[i].length; j++){
                sb.append(","+arr[i][j]);
            }
            request.setAttribute("commodity"+i,sb.toString());
            sb.setLength(0);
        }
        //长度
        request.setAttribute("size",arr.length);

        request.getRequestDispatcher("/WEB-INF/Xiaomi/Product_details.jsp").forward(request,response);
    }
}
